<?php include "counter.php" ?>
 <?php $self_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
  $place = $counter->get_input('place', '', 'get');
  $omit = $counter->get_input('omit', '', 'get');
  $merge_on = $counter->get_input('merge_on', '', 'get');

?>
<html>
<?php pagehead("Find duplicate registrations within $place") ?>
<body bgcolor="white">
<?php pagetop("Find duplicate registrations within $place") ?>

This form is supposed to present all duplicate registrations at country level. Duplicates will be found based on the names of registered people. To start with you have to select the country you want to see the duplicates for. Not using the extra options, this will present all duplicate registrations of the country choosen.<br>
Some people only enter their first or last name in the name field. Using the option "Omit single-word names" removes all duplicates which only have a first or last name in the registration. Chances are small that these registrations will be duplicates of each other. So, only duplicate registrations remain were people added at least a first and last name.<br>
Using the option "Show Merge with .. button/input field" shows an extra table column with a merge button or input field. The button/input field at the end of each row will always merge another key to the key of the row the button/input field belongs to. In the case of an input field you thus have to enter the key number of one of the other duplicates in the same block of duplicates. A merger will always merge the highest registration key into the lowest registration key. So, after a merger the lowest registration key of the two remains.
<hr>
<font color="red">! Before doing a merger, ALWAYS first ask the people of the duplicate registrations by email, whether it really is a duplicate or not !</font>
<hr>
<form action="<?php echo $self_url ?>">
<table width=95% cellspacing=0 cellpadding=0 border=0>
<tr><td align=left>
<?php
if (preg_match("/^Mozilla\/4\./", $HTTP_USER_AGENT)) {
  print "<select name=\"place\" size=5>\n";
} else {
  print "<select name=\"place\">\n";
}

  $q = mysql_query("select name, longname from places where type = 'country'
        order by longname");
  while ($arr = mysql_fetch_array($q)) {
        print "<option value=\"$arr[name]\">$arr[longname]</option>\n";
  }
?>
</select></td>
<td>
<input name="omit" type=checkbox value="single">Omit single-word names
<br>
<input name="merge_on" type=checkbox value="on">Show "Merge with .. " button/input field
</td></tr>
<tr><td>
<br>
<input type="submit" value="Duplicates">
<input type="reset" value="Clear">
</td></tr></table>
</form>

<hr>
<?php
 if (!$columns) {
    $columns = 3;
 }
 if (!$place) {
    $place = "All";
 }

 $placeq = mysql_query("select * from places where name = '$place'");
 $place = mysql_fetch_array($placeq);

?>
<?php place_bar($place); ?>
<table border=0 width="80%">
<tr><td>
<?php print "
<table>
<tr><th align=right>Place code:</th><td>$place[name]</td></tr>
<tr><th align=right>Maintainer(s):</th><td>".findmaintainer($place)."</td></tr>
";
print "</table><p>";

?>
</table>

<!-- Subordinate places -->
<?php

function presentlist($type, $title) {

   global $place;
   global $columns;
   global $PHP_SELF;

   $col = floor($columns/2);
   if ($col == 0) { $col = 1; }

   $places = mysql_query("select * from places where within = $place[f_key]
			and type = '$type' and users > 0 order by longname");
   if (mysql_num_rows($places) == 0) { return; }
   print "<h2>$title within $place[longname]</h2>\n";
   if (mysql_num_rows($places) > 20) {
	$alphaq = mysql_query(
           "select substring(longname, 1, 1) as c from places
                   where within = $place[f_key]
		   and type = '$type' and users > 0
	           group by c");
	print "<h4>";
        while ($alpha = mysql_fetch_row($alphaq)) {
            $a = strtoupper($alpha[0]);
	    print "<a href=\"#place_".$type."_$a\">$a</a> &nbsp; ";
        }
        print "</h4><p>";
   }
   print "<table>\n";
   $preva = "";
   print "<tr>";
   for ($itemcount=0; $itemcount <= $col; $itemcount += 2) {
	print "<th align=left>Name</th><th>Users</th>\n";
   }
   $itemcount = 0;
   while ($sub = mysql_fetch_array($places)) {
       $placeurl = urlencode($sub[name]);
       $a = strtoupper(substr($sub[longname], 0, 1));
       if ($itemcount % $col == 0) {
 	  print "<tr>";
       }
       ++ $itemcount;
       print "<td>";
       if ($a <> $preva) {
	   print "<a name=\"place_".$type."_$a\"> </a>";
           $preva = $a;
       }
       print "<a href=\"$PHP_SELF?place=$placeurl\">$sub[longname]</a>";
       print "<td align=right>$sub[users]<br>\n";
   }
   print "</table>\n";
}
//  presentlist("country", "Countries");
//  presentlist("state", "States/Regions");
//  presentlist("city", "Cities");
?>

<!-- Subordinate persons -->
<?php
  $quotedname = mysql_escape_string($place[name]);

      $persons_query =	"select name,  count(*) as c from persons ".
			"where country = '$quotedname' ".
			"group by name having c > 1 order by name";
      $persons = mysql_query($persons_query);
?>
<p><table width="95%" cellspacing=0 cellpadding=2 border=0>
<?
	if ($omit )  {
		print "<tr><th>Omiting</th></tr>\n";
	}
      $itemcount = 0;
      $linecount = 0;
      $dupcount = 0;
      while ($sub = mysql_fetch_row($persons)) {
	 if ($sub[0] != "" && (! $omit || strchr($sub[0], " ") ) ) {	
	     if ( $sub[1] > 1 ) {
		$esc_name = mysql_escape_string($sub[0]);
              	$dups = mysql_query("select * from persons
                        where country = '$quotedname'
                        and name = '$esc_name'
                        order by name, f_key");
	      $prev_key = ""; 
	      if ( $bgcol == "dddddd" ) {
			$bgcol = "bbbbbb";
	      } else {
			$bgcol = "dddddd";
	      }
?>
<!-- <tr><td colspan=4> -->
<?php
	      $row_count =  mysql_num_rows($dups);
	      if ( $row_count > 1 ) {
	        /* $dupcount++; */
	        while ( $dup = mysql_fetch_array($dups) ) {
	          $linecount++;
		  $dupcount++;
	          print "<tr><td bgcolor=#$bgcol>".
		      "<a href=\"/cgi-bin/adm/display-person.cgi".
		      "?user=$dup[f_key]\">$dup[f_key]</a></td>";
	 	  printf("<td bgcolor=#$bgcol>%s</td><td bgcolor=#$bgcol>%s</td>\n",
				 person_url($dup[f_key], $dup[name]),
				 $dup[placeid]);
		  $u = new User($dup[f_key]);
?>
<td bgcolor=#<?php echo $bgcol ?>><?php echo $u->ufields[state] ?></td>
<td bgcolor=#<?php echo $bgcol ?>><?php echo $u->ufields[email] ?></td>
<?php
		  if ( $prev_key && $merge_on && $dupcount < 3) {
?>
<td>
<form method=post action="/cgi-bin/adm/merge.cgi">
<input type=hidden name="key2" value=<?php echo $prev_key ?>>
<input type=hidden name="key"  value=<?php echo $dup[f_key] ?>>
<input type="submit" value="Merge with <?php echo $prev_key ?>">
</form>
</td></tr>
<?php
		  } else if ( $prev_key && $merge_on) {
?>
<td>
<form method=post action="/cgi-bin/adm/merge.cgi">
Merge with <input name="key2" size=10>
<input type=hidden name="key"  value=<?php echo $dup[f_key] ?>>
<input type="submit" value="Merge">
</form>
</td></tr>
<?php
                  } else {
?>
<td>&nbsp;</td>
</tr>
<?php
		  }
		  $prev_key = $dup[f_key];
		}
		$dupcount = 0;
	      }

	      $itemcount++;
	     }
		//person_url($sub[f_key], $sub[name]));
	}
      }
   //}
?>
</table>
<p>
There are <? echo $itemcount ?> duplicates listed<br>
There are <? echo $linecount ?> Persons listed
</p>

<?php pagebottom() ?>
</body>
</html>

